package com.typhoon.extend.controller;

import java.awt.Menu;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.typhoon.extend.constant.EntityError;
import com.typhoon.extend.constant.ExtendEntityError;
import com.typhoon.extend.dto.AccountDto;
import com.typhoon.extend.dto.UserDto;
import com.typhoon.extend.service.AccountService;
import com.typhoon.extend.service.UserService;
import com.typhoon.extend.util.CommonUtils;
import com.typhoon.extend.util.SessionUtils;

@Controller
@RequestMapping("/user")
public class UserController extends BaseController {
	
    private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);
    
    @Resource
    UserService userService;
    
    @Resource
    AccountService accountService;
    
    @RequestMapping(value = "/query.do")
    public void queryUserByCondition(HttpServletRequest request, HttpServletResponse response) {
    	Long userId = CommonUtils.evalLong(request.getParameter("userId"));
    	if(null == userId || userId <= 0) {
    		this.putParamResult(response, EntityError.ILLEGAL_ARGUMENT_ERROR);
    		return;
    	}
    	try {
			UserDto user = this.userService.queryByPK(userId);
			this.putSuccessResult(response,user);
		} catch (Exception e) {
			LOGGER.error("query user info occur error",e);
			this.putFailtResult(response,"query user info occur error");
		}
    }

	@RequestMapping(value = "/login.do", method = RequestMethod.POST)
	public void login(HttpServletRequest request, @RequestBody Map<String,Object> params, HttpServletResponse response) {
		String accountCode = CommonUtils.evalString(params.get("accountCode"));
		if(StringUtils.isEmpty(accountCode)) {
			this.putParamResult(response, ExtendEntityError.ILLEGAL_ARGUMENT_ERROR);
			return;
		}
		String password = CommonUtils.evalString(params.get("password"));
		if(StringUtils.isEmpty(password)) {
			this.putParamResult(response, ExtendEntityError.PASSWORD_ERROR);
			return;
		}
		
		Subject subject = SecurityUtils.getSubject();
		UsernamePasswordToken token = new UsernamePasswordToken(accountCode, password);
		try {
			//TODO 自定义登录方式
			subject.login(token);
			
//			List<PermissionAppDto> apps = SessionUtils.getApps();
//			this.putSuccessResult(response,"登录授权成功",apps);
			List<Menu> menus = SessionUtils.getMenus();
			this.putSuccessResult(response,"登录授权成功",menus);
		} catch (Exception e) {
			LOGGER.error("",e);
			this.putFailtResult(response,"登录异常");
		}

	}
	
	@RequestMapping(value = "/account-query.do", method = RequestMethod.POST)
	public void queryAccount(@RequestBody Map<String,Object> params, HttpServletResponse response) {
		String accountCode = CommonUtils.evalString(params.get("accountCode"));
		if(StringUtils.isEmpty(accountCode)) {
			this.putParamResult(response, ExtendEntityError.ILLEGAL_ARGUMENT_ERROR);
			return;
		}
		try {
			AccountDto account =  this.accountService.queryByCode(accountCode);
			this.putSuccessResult(response,account);
		} catch (Exception e) {
			LOGGER.error("查询失败",e);
			this.putFailtResult(response,"查询异常");
		}
	}

	
	@RequestMapping(value = "/register.do")
	public String register(HttpServletRequest request, HttpServletResponse response) {
		return "index";
	}

}
